package cn.wolfcode.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.util.concurrent.ScheduledThreadPoolExecutor;

@Configuration
public class RedisConfig {

    /**
     * jdk任务调度线程池
     * @return
     */
    @Bean
    public ScheduledThreadPoolExecutor scheduledThreadPoolExecutor(){
        //核心线程数的配置   核心线程数 = CPU核心数 * 2 + 2
        return new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2);
    }

    /**
     * 加锁-lua脚本：把redis_lock脚本交给Spring容器管理，注册Bean实例
     * @return RedisScript<Boolean>
     */
    @Bean
    public RedisScript<Boolean> lockScript() {
        return RedisScript.of(new ClassPathResource("META-INF/scripts/seckill_lock.lua"),Boolean.class);
    }

    /**
     * 释放锁-lua脚本：把redis_unlock脚本交给Spring容器管理，注册Bean实例
     * @return RedisScript<Boolean>
     */
    @Bean
    public RedisScript<Boolean> unlockScript(){
        return RedisScript.of(new ClassPathResource("META-INF/scripts/seckill_unlock.lua"), Boolean.class);
    }
}
